package com.ncloud.timetask.dashboard.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * @author sundelong
 * project_name: timetask 
 * type_name: DashboardSaleTaskMapper 
 * date: 2017年6月21日下午4:03:47 
 * 
 */
@Mapper
public interface DashboardSaleTaskMapper {
		//当前各销售本月订单金额总数
		@Select(value = ("SELECT a.user_id,b.fee FROM sys_sales a LEFT JOIN (SELECT create_user,SUM(received_fee) AS "
				+ "fee FROM (SELECT create_user,received_fee,create_time FROM ((select discnt_id,discnt_inst_id,"
				+ "create_user,received_fee,create_time from bs_discnt_fee  UNION select c.discnt_id,c.discnt_inst_id"
				+ ",c.create_user,c.received_fee,create_time from bs_o_discnt_fee c,bs_o_order d where c.order_id = "
				+ "d.order_id) a) ) fee WHERE DATE_FORMAT(create_time,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m') GROUP "
				+ "BY create_user) b ON a.user_id = b.create_user  GROUP BY user_id	"))
		public List<Map<Object,Object>> getSlaeCountMonth();
		
		//销售当月销售金额写入月统计表
		@Insert(value = ("insert into bs_sale_count_monthly (task_id,year,month,sale_id,sale_count,update_time,remark) values "
				+ "(#{0},#{1},#{2},#{3},#{4},now(),#{5})"))
		public int insertSaleCount(String taskId,String year,String month,String saleId,String saleCount,String remark);
		
		//更新销售月统计表
		@Update(value=("update bs_sale_count_monthly set sale_count = #{0},update_time=now() where task_id = #{1} and "
				+ "year=#{2} and month=#{3} and sale_id=#{4}"))
		public int updateSaleCount(String saleCount,String taskId,String year,String month,String saleId);
		
		//查询数据是否存在
		@Select(value = (" select count(*) as count from bs_sale_count_monthly where task_id = #{0} and year = #{1} and  month = #{2} and sale_id = #{3}"))
		public int getExistence(String taskId,String year,String month,String saleId);
		
}
